package com.bhs.sansonglogistics.ui.waybill;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.WaybillBean;
import com.bhs.sansonglogistics.bean.WaybillDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.WaybillAdapter;
import com.bhs.sansonglogistics.ui.home.OrderGoodsListFragment;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 装车记录
 */
public class LoadingRecordActivity extends BaseActivity implements NetCallBack {

    private ClipData mClipData;   //剪切板Data对象
    private ClipboardManager mClipboardManager;   //剪切板管理工具类
    private EditText mEtSearch;
    private RecyclerView mRvWaybill;
    private int page = 1;
    private WaybillAdapter mAdapter;
    private final int CONFIRM_ARRIVAL = 102;
    private SwipeRefreshLayout mRefreshLayout;

    @Override
    public int initLayout() {
        return R.layout.activity_loading_record;
    }

    @Override
    public void initView() {
        // 1. 注册mClipboardManager
        mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("装车记录");
        mEtSearch = findViewById(R.id.et_search);
        mRvWaybill = findViewById(R.id.rv_waybill);
        mAdapter = new WaybillAdapter(1);
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索要做的操作
                    page = 1;
                    loadData(mEtSearch.getText().toString());
                    return true;
                }
                return false;
            }
        });

        mRefreshLayout = findViewById(R.id.refreshLayout);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData("");
    }

    @Override
    public void initData() {
        mRvWaybill.setAdapter(mAdapter);
        mRvWaybill.setLayoutManager(new LinearLayoutManager(this));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(mActivity, WaybillDetailsActivity.class)
                        .putExtra("waybill_sn", mAdapter.getData().get(position).getWaybill_sn()));
            }
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                WaybillBean data = mAdapter.getData().get(position);
                if (view.getId() == R.id.btn_adjust) {
                    startActivity(new Intent(mActivity, AdjustWaybillActivity.class).putExtra("waybill_sn", data.getWaybill_sn()));
                } else if (view.getId() == R.id.tv_waybill_number) {
                    //创建一个新的文本clip对象
                    mClipData = ClipData.newPlainText("Simple text", data.getWaybill_sn());
                    //把clip对象放在剪贴板中
                    mClipboardManager.setPrimaryClip(mClipData);
                    ToastUtil.show("复制成功");
                } else {
                    new XPopup.Builder(mActivity)
                            .asConfirm("提示", "确认到达吗？",
                                    "取消", "确定",
                                    new OnConfirmListener() {
                                        @Override
                                        public void onConfirm() {
                                            networkRequest(netApi.waybilConfirmArrival(data.getWaybill_sn()), LoadingRecordActivity.this, CONFIRM_ARRIVAL);
                                        }
                                    }, null, false)
                            .show();
                }
            }
        });
        mAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.layout_empty, null));

        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData("");

            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData("");
            }
        },mRvWaybill);

    }

    private void loadData(String search_key) {
        networkRequest(netApi.getWaybillLis(page, search_key), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            mRefreshLayout.setRefreshing(false);
            WaybillDataBean dataBean = new Gson().fromJson(json, WaybillDataBean.class);
            if (dataBean.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(dataBean.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(dataBean.getData().getList());
                }
                if (mAdapter.getData().size() >= dataBean.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            } else {
                ToastUtil.show(dataBean.getMsg());
            }
        } else {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                page = 1;
                loadData("");
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
    }
}